package com.geek.netty.utils;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

/**
 * @program geek
 * @description: utils
 * @author: chanzhugang
 * @create: 2021/03/31 23:29
 */
public class OkHttpUtils {

    public static OkHttpClient client = new OkHttpClient();

    public static String getAsString(String url) throws IOException {
        Request request = new Request.Builder().url(url).build();

        // try-with resource
        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }

    public static void main(String[] args) throws IOException {
        String url = "http://127.0.0.1:8801/";
        String result = OkHttpUtils.getAsString(url);
        System.out.println("url:" + url + "+++++"+ result);

        //清理资源
        client = null;
    }
}